package com.jlhlyby.num;

import java.lang.reflect.Array;
import java.util.ArrayList;

public class NumberOff {
    public static void main(String[] args) {
        // write your code here
        System.out.println(numberOff(2, 3));
    }

    static ArrayList<Integer> numberOff(int m, int n) {
        //存储输出的数组
        ArrayList<Integer> out = new ArrayList<>();
        //存储下标的数组
        ArrayList<Integer> inArray = new ArrayList<>();
        //填入下标
        for (int i = 0; i < n; i++) {
            inArray.add(i + 1);
        }
        int outIndex = 0;
        int lastIndex = 0;
        while (outIndex < n) {
            //如果剩余人数大于m 则第m就是出列的
            if (inArray.size() >= m+lastIndex) {
                lastIndex = m-1+lastIndex;
                out.add(inArray.get(m - 1));
                inArray.remove(m - 1);
            } else {
                //如果剩余人数小于m 做求余处理
                int index = m % inArray.size();
                index = index == 0 ? inArray.size()-1 : index - 1;
                out.add(inArray.get(index));
                inArray.remove(index);

            }

            outIndex++;
        }
        return out;
    }
}
